<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:252px;">
                    <div class="user-info">
                        <img src="../../assets/img/img.jpg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{name}}</div>
                            <div>{{role}}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        上次登录时间：
                        <span>{{dates | formatDate}}</span>
                    </div>
                    <div class="user-info-list">
                        上次登录地点：
                        <span>{{address}}</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:252px;">
                    <div slot="header" class="clearfix">
                        <span>账户使用记录</span>
                    </div>
                    <el-table  :data="todoList"
                               stripe
                               border
                               height="150"
                               style="width:100%;">
                        <el-table-column prop="ip" label="用户名" align="center" width="70"></el-table-column>
                        <el-table-column  label="时间" align="center" width="200">
                            <template slot-scope="scope">{{scope.row.loginDate |dateFilter("yyyy/mm/dd hh:mm:ss")}}</template>
                        </el-table-column>
                        <el-table-column prop="address" label="地址" align="center"></el-table-column>

                    </el-table>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-people grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{views.viwes}}</div>
                                    <div>访问量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-notice grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{views.userCount}}</div>
                                    <div>用户数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-lx-goods grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{views.companyCount}}</div>
                                    <div>入驻企业</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height:403px;">
                    <schart class="schart" canvasId="pie" :options="options"></schart>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import bus from '../common/bus';
import {formatTimeToStr} from '../common/date.js';
export default {
    name: 'dashboard',
    data() {
        return {
            name: this.$store.getters.getAdmin.aName,
            address:this.$store.getters.getAdmin.loginlog.address,
            dates:this.$store.getters.getAdmin.loginlog.loginDate,
            todoList: [],
            options: {
                type: 'pie',
                title: {
                    text: '入驻企业类型'
                },
                legend: {
                    position: 'left'
                },
                bgColor: '#fbfbfb',
                labels: [],
                datasets: [
                    {
                        data: [2, 1, 1, 2,1, 1, 0]
                    }
                ]
            },
            views:{}
        };
    },
    created() {
        this.getViews();
        this.getCompany();
        this.getViewList();
    },
    components: {
        Schart
    },
    computed: {
        role() {
            return this.$store.getters.getAdmin.state === '1' ? '超级管理员' : '普通用户';
        }
    },
    methods: {
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
            });
        },
        getViews() {
            const _this = this;
            _this.$axios.get('/utils/views').then(res => {
                _this.views =res.data.data;

        });
        },
        getCompany() {
            const _this = this;
            _this.$axios.get("/utils/company").then(res => {
                console.log(res.data.data.list);
                this.options.datasets[0].data=res.data.data.list;
                _this.options.labels=res.data.data.organizationsList;
                 _this.$refs.myChart && _this.$refs.myChart.mergeOptions(_this.options(), true);


        });
        },
        getViewList() {
            const _this = this;
            _this.$axios.get('/utils/viewList'+_this.$store.getters.getAdmin.id).then(res => {
                _this.todoList =res.data.data;

        });
        },

    },
    filters: {
        formatDate: function (time) {
            if (time != null && time != "") {
                var date = new Date(time);
                return formatTimeToStr(date, "yyyy-MM-dd");
            } else {
                return "";
            }
        },

        dateFilter: function (data, format = "") {
            var dt = new Date(data);
            var y = dt.getFullYear();
            var m = (dt.getMonth() + 1).toString().padStart(2, "0");
            var d = dt.getDate().toString().padStart(2, "0");
            var h = dt.getHours().toString().padStart(2, "0");
            var mm = dt.getMinutes().toString().padStart(2, "0");
            var s = dt.getSeconds().toString().padStart(2, "0");
            if (format.toLocaleLowerCase() === "yyyy-mm-dd" ||
                format.toLocaleLowerCase() === "") {
                return `${y}-${m}-${d}`;
            } else if (format.toLocaleLowerCase() === "yyyy/mm/dd") {
                return `${y}/${m}/${d}`;
            } else if (format.toLocaleLowerCase() === "yyyy-mm-dd hh:mm:ss") {
                return `${y}-${m}-${d} ${h}:${mm}:${s}`;
            } else if (format.toLocaleLowerCase() === "yyyy/mm/dd hh:mm:ss") {
                return `${y}/${m}/${d} ${h}:${mm}:${s}`;
            } else {
                return `输入的时间格式有误`;
            }
        }
    }
};
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
